package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.sc.nft.dao.EcologyHatchMethodConfDao;
import com.sc.nft.dto.EcologyHatchConfDao;
import com.sc.nft.dto.UserEcologyHatchRecordsDao;
import com.sc.nft.entity.EcologyHatchConf;
import com.sc.nft.entity.EcologyHatchMethodCollectionConf;
import com.sc.nft.entity.EcologyHatchMethodConf;
import com.sc.nft.entity.EcologyHatchRewardsConf;
import com.sc.nft.entity.req.EditConfigureTheMeltingMode;
import com.sc.nft.entity.req.EditEcologyHatchMethodCollectionConf;
import com.sc.nft.entity.vo.AdminConfigureTheMeltingModeListVO;
import com.sc.nft.entity.vo.AdminConfigureTheMeltingModeVO;
import com.sc.nft.entity.vo.AdminEcologyHatchMethodCollectionConfVO;
import com.sc.nft.entity.vo.AdminSmeltingRewardsConfVO;
import com.sc.nft.entity.vo.ecology.HatchMethodCollectionVO;
import com.sc.nft.entity.vo.ecology.HatchMethodVO;
import com.sc.nft.entity.vo.ecology.HatchPageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologyHatchMethodCollectionConfService;
import com.sc.nft.service.EcologyHatchRewardsConfService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.EcologyHatchMethodConfService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 生态-熔炼方式主表服务接口实现
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-06-07 09:04:10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchMethodConfServiceImpl implements EcologyHatchMethodConfService {

    private final EcologyHatchMethodConfDao ecologyHatchMethodConfDao;
    private final EcologyHatchMethodCollectionConfService ecologyHatchMethodCollectionConfService;
    private final EcologyHatchRewardsConfService ecologyHatchRewardsConfService;
    private final EcologyHatchConfDao ecologyHatchConfDao;
    private final UserEcologyHatchRecordsDao userEcologyHatchRecordsDao;

    /**
     * 添加熔炼方式
     *
     * @param ecologyHatchId                熔炼配置id
     * @param configureTheMeltingModeVOList 熔炼方式列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEcologyHatchMethodConf(Long ecologyHatchId, Boolean consumptionConsensus, List<EditConfigureTheMeltingMode> configureTheMeltingModeVOList) {

        EcologyHatchConf ecologyHatchConfInDb = ecologyHatchConfDao.getById(ecologyHatchId);
        Assert.notNull(ecologyHatchConfInDb, () -> new GlobalRunTimeException("生态熔炼配置不存在"));
        Assert.isFalse(ecologyHatchConfInDb.getStatus(), () -> new GlobalRunTimeException("生态熔炼配置已经启用，不可修改"));
        if (!ecologyHatchConfInDb.getConsumptionConsensus().equals(consumptionConsensus)) {
            //  修改是否消耗共识
            ecologyHatchConfDao.updateConsumptionConsensusById(ecologyHatchId, consumptionConsensus);
        }
        List<Boolean> methodStatus = configureTheMeltingModeVOList.stream().map(editConfigureTheMeltingMode -> editConfigureTheMeltingMode.getStatus()).collect(Collectors.toList());
        if (!methodStatus.contains(Boolean.TRUE)) {
            throw new GlobalRunTimeException("至少保留一条熔炼方式开启状态");
        }
        for (EditConfigureTheMeltingMode editConfigureTheMeltingMode : configureTheMeltingModeVOList) {
            /*StringBuffer methodName = new StringBuffer();
            if (StringUtils.isBlank(editConfigureTheMeltingMode.getMethodName())){
                List<EditEcologyHatchMethodCollectionConf> ecologyHatchMethodCollectionConfVOList = editConfigureTheMeltingMode.getEcologyHatchMethodCollectionConfVOList();
                List<String> collectionNameList = ecologyHatchMethodCollectionConfVOList.stream().map(EditEcologyHatchMethodCollectionConf::getCollectionName).collect(Collectors.toList());
                String collectionName = String.join("+", collectionNameList);
                methodName = methodName.append(ecologyHatchConfInDb.getCollectionName()).append("+").append(collectionName);
            }else {
                methodName = methodName.append(editConfigureTheMeltingMode.getMethodName());
            }*/
            // 获取熔炼方式id
            Long methodId = editConfigureTheMeltingMode.getId();
            // 熔炼方式
            if (Objects.isNull(methodId)) {
                EcologyHatchMethodConf ecologyHatchMethodConf = new EcologyHatchMethodConf();
                ecologyHatchMethodConf.setEcologyHatchId(ecologyHatchId);
                ecologyHatchMethodConf.setSort(editConfigureTheMeltingMode.getSort());
                ecologyHatchMethodConf.setStatus(editConfigureTheMeltingMode.getStatus());
                ecologyHatchMethodConf.setMethodName(editConfigureTheMeltingMode.getMethodName());
                ecologyHatchMethodConf.setDynamicRewardAmount(editConfigureTheMeltingMode.getDynamicRewardAmount());
                ecologyHatchMethodConf.insert();
                methodId = ecologyHatchMethodConf.getId();
            } else {
                Assert.isTrue(ecologyHatchMethodConfDao.updateById(methodId, editConfigureTheMeltingMode.getSort(),
                        editConfigureTheMeltingMode.getStatus(), editConfigureTheMeltingMode.getMethodName(),editConfigureTheMeltingMode.getDynamicRewardAmount()), () -> new GlobalRunTimeException("修改熔炼方式失败"));
            }
            // 编辑熔炼方式藏品关联表
            ecologyHatchMethodCollectionConfService.editEcologyHatchMethodCollectionConf(ecologyHatchId, methodId, editConfigureTheMeltingMode.getEcologyHatchMethodCollectionConfVOList(),ecologyHatchConfInDb.getCollectionId());
            // 编辑奖励
            ecologyHatchRewardsConfService.editEcologyHatchRewardsConf(ecologyHatchId, methodId, editConfigureTheMeltingMode.getRewardsConfRequestList());
        }
    }

    /**
     * 获取熔炼方式关联详情
     *
     * @param id
     * @return
     */
    @Override
    public AdminConfigureTheMeltingModeListVO getAdminConfigureTheMeltingModeListVO(Long id) {
        EcologyHatchConf ecologyHatchConf = ecologyHatchConfDao.getById(id);
        List<EcologyHatchMethodConf> ecologyHatchMethodConfList = ecologyHatchMethodConfDao.getListByEcologyHatchId(id, null);
        AdminConfigureTheMeltingModeListVO adminConfigureTheMeltingModeListVO = new AdminConfigureTheMeltingModeListVO();
        adminConfigureTheMeltingModeListVO.setEcologyHatchId(id);
        adminConfigureTheMeltingModeListVO.setConsumptionConsensus(ecologyHatchConf.getConsumptionConsensus());
        List<AdminConfigureTheMeltingModeVO> adminConfigureTheMeltingModeVOList = new ArrayList<>();
        for (EcologyHatchMethodConf ecologyHatchMethodConf : ecologyHatchMethodConfList) {
            AdminConfigureTheMeltingModeVO adminConfigureTheMeltingModeVO = new AdminConfigureTheMeltingModeVO();
            adminConfigureTheMeltingModeVO.setId(ecologyHatchMethodConf.getId());
            adminConfigureTheMeltingModeVO.setSort(ecologyHatchMethodConf.getSort());
            adminConfigureTheMeltingModeVO.setStatus(ecologyHatchMethodConf.getStatus());
            adminConfigureTheMeltingModeVO.setMethodName(ecologyHatchMethodConf.getMethodName());
            adminConfigureTheMeltingModeVO.setDynamicRewardAmount(ecologyHatchMethodConf.getDynamicRewardAmount());

            // 获取熔炼方式奖励关联表
            List<EcologyHatchRewardsConf> ecologyHatchRewardsConfList = ecologyHatchRewardsConfService.getList(ecologyHatchMethodConf.getId(), id);
            // 获取熔炼方式藏品关联表
            List<EcologyHatchMethodCollectionConf> ecologyHatchMethodCollectionConfList = ecologyHatchMethodCollectionConfService.getList(ecologyHatchMethodConf.getId(), id);
            adminConfigureTheMeltingModeVO.setEcologyHatchMethodCollectionConfVOList(BeanUtil.copyToList(ecologyHatchMethodCollectionConfList, AdminEcologyHatchMethodCollectionConfVO.class));
            adminConfigureTheMeltingModeVO.setRewardsConfRequestList(BeanUtil.copyToList(ecologyHatchRewardsConfList, AdminSmeltingRewardsConfVO.class));
            adminConfigureTheMeltingModeVOList.add(adminConfigureTheMeltingModeVO);
        }
        adminConfigureTheMeltingModeListVO.setAdminConfigureTheMeltingModeVOList(adminConfigureTheMeltingModeVOList);
        return adminConfigureTheMeltingModeListVO;
    }

    /**
     * 获取熔炼方式详情
     *
     * @param ecologyHatchId
     * @return
     */
    @Override
    public List<HatchMethodVO> getHatchMethodVO(Long userId, Long ecologyHatchId, HatchPageVO hatchPageVO) {
        List<HatchMethodVO> hatchMethodVOList = new ArrayList<>();
        // 获取开启的熔炼方式列表
        List<EcologyHatchMethodConf> ecologyHatchMethodConfList = ecologyHatchMethodConfDao.getListByEcologyHatchId(ecologyHatchId, Boolean.TRUE);
        // 获取熔炼配置中所有的藏品关联表
        List<HatchMethodCollectionVO> hatchMethodCollectionVOList = ecologyHatchMethodCollectionConfService.getHatchMethodCollectionVO(userId, ecologyHatchId);
        for (EcologyHatchMethodConf ecologyHatchMethodConf : ecologyHatchMethodConfList) {
            HatchMethodVO hatchMethodVO = new HatchMethodVO();
            hatchMethodVO.setMethodId(ecologyHatchMethodConf.getId());
            List<HatchMethodCollectionVO> voList = hatchMethodCollectionVOList.stream().filter(hatchMethodCollectionVO -> hatchMethodCollectionVO.getMethodId().equals(ecologyHatchMethodConf.getId())).collect(Collectors.toList());
//            HatchMethodCollectionVO vo = HatchMethodCollectionVO.build(hatchPageVO, ecologyHatchMethodConf.getId());
//            voList.add(vo);
            hatchMethodVO.setHatchMethodCollectionVOList(voList);
            hatchMethodVOList.add(hatchMethodVO);
        }
        return hatchMethodVOList;
    }

    @Override
    public EcologyHatchMethodConf getById(Long id) {
        return ecologyHatchMethodConfDao.getById(id);
    }
}